<?php
namespace app\api\controller\vppz;


use think\Cache;


use \app\admin\model\vppz\Bjjy as BjjyModel;
use \app\admin\model\vppz\KeshiHospital as KeshiHospitalModel;
use \app\admin\model\vppz\Keshi as KeshiModel;
use \app\admin\model\vppz\Slide as SlideModel;
use \app\admin\model\vppz\Hospital as HospitalModel;
use \addons\vppz\library\Vpower;
use function fast\e;

class Bjjy extends App
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize(){
        parent::_initialize();
        $this->BjjyModel = new BjjyModel;
        $this->KeshiHospitalModel = new KeshiHospitalModel;
        $this->SlideModel = new SlideModel;
        $this->KeshiModel = new KeshiModel;
        $this->HospitalModel = new HospitalModel;
    }


    /**
     * 北京就医列表
     */
    public function bjjylists(){
		$area=$this->_area;
		$mine = $this->_mine;
        // 幻灯
		$slides = $this->SlideModel->field('id,pic_image,stype,stype_link,title')
            ->where(['app_id'=>$this->app_id,'use_switch'=>1,'area_id'=>$area['id'],'channel'=>3])
            ->order('weigh', 'desc')->select();

        $bjjy = $this->BjjyModel->whereNull('deletetime');
        $keyword = input('keyword');
        if (empty($keyword) === FALSE){
            $bjjy = $bjjy->whereLike('name|desc','%'.$keyword.'%');
        }
        $bjjy = $bjjy->order('weigh','DESC')->select();
        if(empty($bjjy) === FALSE){
            foreach ($bjjy as $bk=>$bj){
                $bjjy[$bk]['createtime'] = date('Y-m-d H:i:s',$bjjy[$bk]['createtime']);
                //获取重点科室
                $keshiHospital = $this->KeshiHospitalModel->with(['yyks'])->where(array(
                    'hospitalid'=>$bj['hospital_id']
                ))->select();
                foreach ($keshiHospital as $row) {
                    $row->getRelation('yyks')->visible(['name']);
                }
                $bjjy[$bk]['keshi'] =  $keshiHospital;
            }
        }
		$this->success('',array(
			'now'=>time(),
			'bjjy'=>$bjjy,
            'slide'=>$slides
		));
    }

    /**
     * 北京就医详情
     */
    public function detail(){

		$id = input('id');

		$bjjy = $this->BjjyModel->find($id);
		if(empty($bjjy)){
			$this->error('不存在或暂停提供');
		}
        $bjjy['createtime'] = date('Y-m-d H:i:s',$bjjy['createtime']);
        $keshiHospital = $this->KeshiHospitalModel->with(['yyks'])->where(array(
            'hospitalid'=>$bjjy['hospital_id']
        ))->select();
        $hospital = $this->HospitalModel->find($bjjy['hospital_id']);
        $bjjy['keshi'] = [];
        if(empty($keshiHospital) === FALSE) {
            foreach ($keshiHospital as $row) {
                $row->getRelation('yyks')->visible(['name']);
            }

            $bjjy['keshi'] = $keshiHospital;
        }
		$this->success('',array(
			'now'=>time(),
			'bjjy'=>$bjjy,
            'hospital'=>$hospital
		));
    }
/**
     * 北京就医详情
     */
    public function ksdetail(){

		$id = input('id');

		$bjjy = $this->KeshiHospitalModel->find($id);
		if(empty($bjjy)){
			$this->error('不存在或暂停提供');
		}
        $bjjy['createtime'] = date('Y-m-d H:i:s',$bjjy['createtime']);
        $bjjy['fileData'] = [];
        if(empty($bjjy['file']) === FALSE){
            $filesData = [];
            $files = explode(',',$bjjy['file']);
            foreach ($files as &$f){
                $f =  \addons\vppz\library\Vpower::dourl($f);
                $path = parse_url($f, PHP_URL_PATH);
                $info = pathinfo($path); // 获取路径信息
                $filename = $info['basename']; // 获取文件名
                array_push($filesData,array(
                    'path'=>$f,
                    'name'=>$filename
                ));
            }
            $bjjy['fileData'] = $filesData;
        }
		$this->success('',array(
			'now'=>time(),
			'bjjy'=>$bjjy
		));
    }
}
